package logica;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class MyDataBaseHelper extends SQLiteOpenHelper {
	 public int GetCursor;
	 private Context myContext;
	 public String DB_PATH = "/JogoDo24/assets/"; 
	 public static String DB_NAME = "bdCartas";
	 static String ASSETS_DB_FOLDER = "db";
	 private SQLiteDatabase db;

	 public MyDataBaseHelper(Context context) {
	    super(context, DB_NAME, null, 1);
	    if (db != null && db.isOpen())
	        close();

	    this.myContext = context;

	    try {
	        createDataBase();
	        openDataBase();
	    } catch (IOException e) {
	        // System.out.println("Exception in creation of database : "+
	        // e.getMessage());
	        e.printStackTrace();
	    }

	}

	public void createDataBase() throws IOException {
	    boolean dbExist = checkDataBase();

	    if (dbExist) {
	        // System.out.println("Database Exist");
	    } else {
	        this.getReadableDatabase();

	        try {
	            copyDatabase();
	        } catch (IOException e) {
	            throw new Error("Error copying database");
	        }
	    }
	}

	private void copyDatabase() throws IOException {
	    InputStream input = myContext.getAssets().open(DB_NAME);
	    String outputFileName = DB_PATH + DB_NAME;
	    OutputStream output = new FileOutputStream(outputFileName);

	    byte[] buffer = new byte[1024];
	    int length;
	    while ((length = input.read(buffer)) > 0) {
	        output.write(buffer, 0, length);
	    }

	    // Close the streams
	    output.flush();
	    output.close();
	    input.close();
	    // System.out.println(DB_NAME + "Database Copied !");
	}

	@Override
	public void onCreate(SQLiteDatabase db) {

	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

	}

	public void openDataBase() throws SQLException {
	    // Open the database
	    String myPath = DB_PATH + DB_NAME;
	    db = SQLiteDatabase.openDatabase(myPath, null,
	            SQLiteDatabase.OPEN_READONLY);
	}

	public boolean isOpen() {
	    if (db != null)
	        return db.isOpen();
	    return false;
	}

	@Override
	public synchronized void close() {
	    if (db != null)
	        db.close();
	    super.close();
	}

	private boolean checkDataBase() {
	    SQLiteDatabase checkDB = null;
	    try {
	        String myPath = DB_PATH + DB_NAME;
	        // System.out.println("My Pathe is:- " + myPath);
	        // System.out.println("Open");
	        checkDB = SQLiteDatabase.openDatabase(myPath, null,
	                SQLiteDatabase.OPEN_READWRITE);
	        // System.out.println("checkDB value:" + checkDB);
	        // System.out.println("My Pathe is:- " + myPath);
	    } catch (Exception e) {
	        // database does't exist yet.
	    }

	    if (checkDB != null) {
	        // System.out.println("Closed");
	        checkDB.close();
	        // System.out.println("My db is:- " + checkDB.isOpen());
	    }

	    return checkDB != null ? true : false;
	}

	public Cursor execCursorQuery(String sql) {
	    Cursor cursor = null;
	    try {
	        cursor = db.rawQuery(sql, null);
	        GetCursor = cursor.getCount();
	        Log.i("Inside execCursorQuery try", sql);
	    } catch (Exception e) {
	        Log.i("Inside execCursorQuery exception", e.getMessage());
	    }
	    return cursor;
	}

	public void execNonQuery(String sql) {
	    try {
	        db.execSQL(sql);
	        // Log.d("SQL", sql);
	    } catch (Exception e) {
	        // Log.e("Err", e.getMessage());
	    } finally {
	        // closeDb();
	    }
	}}